import 'package:color_dart/RgbaColor.dart';
import 'package:flutter/material.dart';
import 'package:im_flutter_sdk_example/components/activity/activity_list_item.dart';
import 'package:im_flutter_sdk_example/jsonserialize/active/data.dart';
import 'package:im_flutter_sdk_example/utils/global.dart';

class ActiveList extends StatefulWidget {
  final isRecommend;
  ActiveList({Key key, this.isRecommend}) : super(key: key);
  @override
  _ActiveListState createState() => _ActiveListState();
}

class _ActiveListState extends State<ActiveList>
    with AutomaticKeepAliveClientMixin {
  List<ActiveData> activeData = [];
  @override
  void initState() {
    super.initState();
    this.getData();
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return SingleChildScrollView(
      child: Container(
        color: rgba(249, 249, 249, 1),
        child: Column(
          children: [
            Container(
              child: widget.isRecommend == true && activeData.length > 2
                  ? Column(
                      children: activeData
                          .getRange(0, 3)
                          .map((item) => ActivityListItem(item: item))
                          .toList(),
                    )
                  : Column(
                      children: activeData
                          .map((item) => ActivityListItem(item: item))
                          .toList(),
                    ),
            )
          ],
        ),
      ),
    );
  }

  @override
  bool get wantKeepAlive => true;
  //获取活动列表数据
  void getData() async {
    G.showLoading(status: '加载中...');
    var res = await G.req.home.activityList();
    print(res);
    res.data['data']['list'].forEach((e) {
      ActiveData active = ActiveData.fromJson(e);
      activeData.add(active);
    });

    this.setState(() {});
    G.dismissLoading();
  }
}
